#include "areaconfigureview.h"
#include "ui_areaconfigureview.h"
#include "dboperate.h"

AreaConfigureView::AreaConfigureView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AreaConfigureView)
{
    ui->setupUi(this);
    setWindowTitle("监控区域配置");
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color: rgb(200, 255, 255);border: none;height:28px;}");
    ui->tableWidget->verticalHeader()->setHidden(true);
}

AreaConfigureView::~AreaConfigureView()
{
    delete ui;
}

void AreaConfigureView::on_addButton_clicked()
{
    ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1);
}

void AreaConfigureView::setTable(QVector<AreaInfo> areasInfo)
{
    ui->tableWidget->setRowCount(areasInfo.length());
    for (int i = 0; i < areasInfo.length(); i++)
    {
        QString areaName = areasInfo.at(i).areaName;
        QString areaPoint = areasInfo.at(i).point;
        QString areaImage = areasInfo.at(i).imagePath;
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(areaName));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(areaPoint));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(areaImage));
    }
}

void AreaConfigureView::on_removeButton_clicked()
{
    int currentRow = ui->tableWidget->currentRow();
    if (currentRow < 0)
    {
        return;
    }
    ui->tableWidget->removeRow(currentRow);
}

void AreaConfigureView::on_applyButton_clicked()
{
    QVector<AreaInfo> areas;
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        AreaInfo area;
        area.areaName = ui->tableWidget->item(i, 0)->text();
        area.point = ui->tableWidget->item(i, 1)->text();
        area.imagePath = ui->tableWidget->item(i, 2)->text();
        areas.append(area);
    }
    DBOperate db;
    db.updateAreas(areas);
}
